package com.jiyun.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.jiyun.dto.AnnouncementDto;
import com.jiyun.service.IAnnouncementService;
import com.jiyun.model.xitong.Announcement;
import com.jiyun.model.xitong.dto.AnnouncementQuery;
import com.jiyun.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 【-Announcement】操作接口
 *
 * @author ???
 * @since 2024-09-26
 */
@RestController
@RequestMapping("/announcement")
@Api(tags = "操作接口")
@CrossOrigin
public class AnnouncementController {

    @Autowired
    public IAnnouncementService announcementService;

    /**
     * 接口：添加
     * @param announcement 要添加的
     * @return Result 响应给前端
     */
    @PostMapping
    @ApiOperation("添加" )
    public Result save(@RequestBody Announcement announcement) {
        announcement.setStatus(0);
        announcement.setEditTime(LocalDateTime.now());
        announcement.setOperationTime(LocalDateTime.now());
        announcement.setUserId(1);
        announcement.setDoctorId(2);
        announcementService.save(announcement);
        return Result.ok();
    }

    /**
     * 接口：根据Id删除
     * @param id 的Id
     * @return Result 响应给前端
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据Id删除")
    public Result remove(@PathVariable("id" ) Long id) {
        announcementService.removeById(id);
        return Result.ok();
    }

    /**
     * 接口：修改
     * @param announcement 要修改的数据
     * @return Result 响应给前端
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody Announcement announcement) {
        announcementService.updateById(announcement);
        return Result.ok();
    }


    /**
      * 根据Id查询单个
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询单个")
    public Result getById(@PathVariable Long id)  {
        Announcement one = announcementService.getById(id);
        return Result.ok(one);
    }

    /**
     * 接口：查询所有
     */
    @GetMapping
    @ApiOperation("查询所有")
    public Result getList() {
        List<Announcement> listAll = announcementService.list();
        return Result.ok(listAll);
    }


    /**
     * 接口：分页查询所有
     * @return List<Announcement> 所有数据
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation("获取所有")
    public Result getPageList(@PathVariable Integer page, @PathVariable Integer limit, AnnouncementQuery query) {
        //封装分页参数
        query.setCurrentPage(page);
        query.setPageSize(limit);
        //查询分页数据
        IPage<Announcement> pageList = announcementService.getPageList(query);
        return Result.ok(pageList);
    }
    @PostMapping("/findAllAnnouncement")
    @ApiOperation("查询所有信息+时间区间查询+分页")
    public Result findAllAnnouncement(@RequestBody AnnouncementDto announcementDto){
        IPage<Announcement> pageList = announcementService.findAllAnnouncement(announcementDto);
        return Result.ok(pageList);
    }
    @GetMapping("/exportExcel")
    @ApiOperation("导出excel")
    public Result exportExcel(Integer[] ids) throws IOException {
        List<Announcement> list = new ArrayList<>();
        for (Integer id : ids) {
            Announcement byId = announcementService.getById(id);
            list.add(byId);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Announcement.class, list);
        String url="D:\\xls";
        File file = new File(url);
        if (!file.exists()){
            file.mkdir();
        }
        workbook.write(new FileOutputStream(new File(url+"\\announcement.xls")));
        workbook.close();
        return Result.ok();
    }
}
